package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.domain.MessageCenter;
import com.ruoyi.business.domain.vo.AuthBindingInfoReviewVO;
import com.ruoyi.business.domain.vo.CommonReviewVO;
import com.ruoyi.business.service.IMessageCenterService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.WxUserFace;
import com.ruoyi.business.service.IWxUserFaceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人脸识别管理Controller
 * 
 * @author ruoyi
 * @date 2021-11-27
 */
@RestController
@RequestMapping("/business/face")
public class WxUserFaceController extends BaseController
{
    @Autowired
    private IWxUserFaceService wxUserFaceService;

    @Autowired
    private IMessageCenterService messageCenterService;

    /**
     * 查询人脸识别管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:face:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxUserFace wxUserFace)
    {
        startPage();
        List<WxUserFace> list = wxUserFaceService.selectWxUserFaceList(wxUserFace);
        return getDataTable(list);
    }

    /**
     * 导出人脸识别管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:face:export')")
    @Log(title = "人脸识别管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WxUserFace wxUserFace)
    {
        List<WxUserFace> list = wxUserFaceService.selectWxUserFaceList(wxUserFace);
        ExcelUtil<WxUserFace> util = new ExcelUtil<WxUserFace>(WxUserFace.class);
        return util.exportExcel(list, "人脸识别管理数据");
    }

    /**
     * 获取人脸识别管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:face:query')")
    @GetMapping(value = "/{faceId}")
    public AjaxResult getInfo(@PathVariable("faceId") Long faceId)
    {
        return AjaxResult.success(wxUserFaceService.selectWxUserFaceByFaceId(faceId));
    }

    /**
     * 新增人脸识别管理
     */
    @PreAuthorize("@ss.hasPermi('business:face:add')")
    @Log(title = "人脸识别管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxUserFace wxUserFace)
    {
        return toAjax(wxUserFaceService.insertWxUserFace(wxUserFace));
    }

    /**
     * 修改人脸识别管理
     */
    @PreAuthorize("@ss.hasPermi('business:face:edit')")
    @Log(title = "人脸识别管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxUserFace wxUserFace)
    {
        return toAjax(wxUserFaceService.updateWxUserFace(wxUserFace));
    }

    /**
     * 删除人脸识别管理
     */
    @PreAuthorize("@ss.hasPermi('business:face:remove')")
    @Log(title = "人脸识别管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{faceIds}")
    public AjaxResult remove(@PathVariable Long[] faceIds)
    {
        return toAjax(wxUserFaceService.deleteWxUserFaceByFaceIds(faceIds));
    }

    /**
     * 审核人脸识别
     */
    @Log(title = "审核人脸识别", businessType = BusinessType.INSERT)
    @PostMapping("/review")
    public AjaxResult review(@RequestBody CommonReviewVO commonReviewVO)
    {
        int i = wxUserFaceService.reviewUserFace(commonReviewVO);
        if (i == 0) {
            return AjaxResult.error("人脸审核失败，请检查该用户小区是否绑定了人脸识别设备");
        }
        MessageCenter messageCenter = new MessageCenter();
        messageCenter.setTitle("人脸识别审核结果");
        String content = "人脸审核通过,感谢您的使用";
        if (commonReviewVO.getStatus().equals("0")) {
            content = "审核被驳回,驳回理由：" + commonReviewVO.getContent();
        }
        messageCenter.setContent("[人脸识别]" + content);
        messageCenter.setIsRead("0");
        messageCenter.setModule("人脸识别");
        messageCenter.setDestUserId(commonReviewVO.getUserId().toString());
        messageCenterService.insertMessageCenter(messageCenter);
        return AjaxResult.success();
    }

    @Log(title = "手动下发人脸", businessType = BusinessType.INSERT)
    @GetMapping("/dispatch_manual")
    public AjaxResult dispatchManual(String faceId) {
        boolean b = wxUserFaceService.dispatchManual(faceId);
        if (!b) {
            return AjaxResult.error("下发失败");
        }
        return AjaxResult.success();
    }

    @Log(title = "根据小区统一手动下发人脸", businessType = BusinessType.INSERT)
    @GetMapping("/dispatch_manual_village")
    public AjaxResult dispatchManualVillage(String villageId) {
        if (villageId == null) {
            return AjaxResult.success();
        }
        WxUserFace wxUserFace = new WxUserFace();
        wxUserFace.setVillageId(villageId);
        List<WxUserFace> wxUserFaces = wxUserFaceService.selectWxUserFaceList(wxUserFace);
        wxUserFaces.forEach(wxUserFace1 -> {
            wxUserFaceService.dispatchManual(wxUserFace1.getFaceId().toString());
        });
        return AjaxResult.success();
    }

}
